package dashboard.controllers;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.argo.ActionResult;
import com.bj58.argo.annotations.POST;
import com.bj58.argo.annotations.Path;
import com.bj58.argo.controller.AbstractController;

import dashboard.cache.ServiceBeanFactory;
import dashboard.constant.EnumCate;
import dashboard.constant.EnumPlatform;
import dashboard.constant.NetFlowIndex;
import dashboard.databll.NetFlowBLL;
import dashboard.dbentity.NetFlowSource;
import dashboard.service.AbstractBaseService;
import dashboard.utils.DateUtil;
import dashboard.utils.MyServletRequestUtils;

@Path("/netflow")
public class NetFlowAjaxController extends AbstractController {
//	private AbstractBaseService<NetFlow> nfService =BeanFactory.get(NetFlow.class,null);
//	private AbstractBaseService<NetFlowCity> nfCityService = BeanFactory.get(NetFlowCity.class,null);
//	private AbstractBaseService<SearchWord> searchWordService = BeanFactory.get(SearchWord.class,null);
	private AbstractBaseService<NetFlowSource> nfSourceService = ServiceBeanFactory.get(NetFlowSource.class,null);
	
	@POST
	@Path("/ajax/{cate:\\S+}/{index:\\S+}")
	public ActionResult getDataByCateIndex(String cate, String index) {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			//index参数不合法时会有illegalArgumentException异常
			indexEnum = NetFlowIndex.valueOf(index);
			cateEnum = EnumCate.valueOf(cate);
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String data = NetFlowBLL.getLine_Day(cateEnum, indexEnum,dateGroup);
//			log.info(cate+"--"+index+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return view("null");
		}
	}
	
	
	@Path("/ajax/platform/{cate:\\S+}/{index:\\S+}")
	@POST
	public ActionResult getPlatformData(String cate, String index) {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
//			Model model = beat().getModel();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			indexEnum = NetFlowIndex.valueOf(index);
			cateEnum = EnumCate.valueOf(cate);

			String data = NetFlowBLL.getPlatformJson(cateEnum, indexEnum,dateGroup);
//			log.info(cate+"--platform--"+index+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.jsonView("{}");
		}
	}
	
	/**
	 * 获取 “三端流量分别占总三端的比值” 数据的data
	 * @param cate
	 * @param index
	 * @return
	 */
	@Path("/ajax/platformRate")
	@POST
	public ActionResult getPlatformRateData() {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");

			indexEnum = NetFlowIndex.valueOf(searchIndex);
			cateEnum = EnumCate.valueOf(businessName);

			String data = NetFlowBLL.getPlatformRateJson(cateEnum, indexEnum,dateGroup);
//			log.info(businessName+"-"+indexEnum+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.jsonView("{}");
		}
	}
	
	
	@Path("/ajax/cateTotal/{cate:\\S+}/{index:\\S+}")
	@POST
	public ActionResult getCateTotalData(String cate, String index) {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			indexEnum = NetFlowIndex.valueOf(index);
			cateEnum = EnumCate.valueOf(cate);
			HttpServletRequest request = beat().getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String data = NetFlowBLL.getCateTotal_DayJson(cateEnum,indexEnum,dateGroup);
			
//			log.info("cateTotal--"+index+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.jsonView("{}");
		}

	}
	
	/**
	 * 全站top UV 热力图
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-02-09
	 */
	@Path("/ajax/heatCityUV")
	@POST
	public ActionResult topNCity() {
		try{
			HttpServletRequest request = beat().getRequest();
			//平台
			String plat = MyServletRequestUtils.getStringParameter(request, "platform", EnumPlatform.all.getColName());
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			//日期
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date", "");
			if(StringUtils.isBlank(stat_date)){
				stat_date = DateUtil.getYestoday("yyyy-MM-dd");
			}
			
			String jsonArray = NetFlowBLL.getUVCityJson(stat_date,platEnum, cateEnum);
			return ViewResult.jsonView(jsonArray);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("[]");
		}
	}
	
	@Path("/ajax/source")
	@POST
	public ActionResult getSourceData() {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String plat = MyServletRequestUtils.getStringParameter(request, "platform", "pc");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
			String sourceField = MyServletRequestUtils.getStringParameter(request, "sourceLevel", "source");
			
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			indexEnum = NetFlowIndex.valueOf(searchIndex);
			cateEnum = EnumCate.valueOf(businessName);

			String data = NetFlowBLL.getSourceDataJson(cateEnum, indexEnum,platEnum,sourceField,dateGroup);
//			log.info(businessName+"--platform--"+plat+"--"+searchIndex+"---"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
	}

	@Path("/ajax/sourceTwo")
	@POST
	public ActionResult getSourceTwoData() {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String plat = MyServletRequestUtils.getStringParameter(request, "platform", "pc");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
			String source1_name = MyServletRequestUtils.getStringParameter(request, "source1_name", "SEO");
			
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			indexEnum = NetFlowIndex.valueOf(searchIndex);
			cateEnum = EnumCate.valueOf(businessName);

			String data = NetFlowBLL.getSourceDataTwoJson(cateEnum, indexEnum,platEnum,source1_name,dateGroup);
//			log.info(businessName+"--platform--"+plat+"--"+searchIndex+"---"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
	}

	@Path("/ajax/sourceThree")
	@POST
	public ActionResult getSourceThreeData() {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
			String source2_name = MyServletRequestUtils.getStringParameter(request, "source2_name", "付费浏览器");
			
			indexEnum = NetFlowIndex.valueOf(searchIndex);
			cateEnum = EnumCate.valueOf(businessName);

			String data = NetFlowBLL.getSourceDataThreeJson(cateEnum, indexEnum,source2_name,dateGroup);
//			log.info(businessName+"--platform--"+plat+"--"+searchIndex+"---"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
	}
	
	@Path("/ajax/sourceTwoPie")
	@POST
	public ActionResult getSourceTwoPieData() {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate", "2015-01-01");
			String endDate = MyServletRequestUtils.getStringParameter(request, "endDate", "2015-01-01");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String plat = MyServletRequestUtils.getStringParameter(request, "platform", "pc");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
			
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			indexEnum = NetFlowIndex.valueOf(searchIndex);
			cateEnum = EnumCate.valueOf(businessName);
			if(StringUtils.isBlank(beginDate)){
				beginDate = nfSourceService.getMinDate();
			}
			if(StringUtils.isBlank(endDate)){
				endDate = nfSourceService.getMaxDate();
			}
			
			String data = NetFlowBLL.getSourceTwoPieDataJson(cateEnum, indexEnum,platEnum,beginDate,endDate);
//			log.info(businessName+"--platform--"+plat+"--"+searchIndex+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
		
	}
	@Path("/ajax/cityCateTwo")
	@POST
	public ActionResult getCityCateData() {
		NetFlowIndex indexEnum = null;
		EnumCate busEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String city1 = MyServletRequestUtils.getStringParameter(request, "city1","all");
			String cate2 = MyServletRequestUtils.getStringParameter(request, "cate2","");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
			
			indexEnum = NetFlowIndex.valueOf(searchIndex);
			busEnum = EnumCate.valueOf(businessName);
			String data = NetFlowBLL.getCityCate2Json(busEnum,city1,cate2,indexEnum,dateGroup);
//			log.info(businessName+":"+city1+":"+searchIndex+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("{}");
		}
	}
	
	
}
